﻿using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Sprite 2D的角度,0在正北方
/// </summary>
public class AutoRuling : MonoBehaviour {

    public GaugeInfo gauge;

    public Vector3 RulingScale;

    public GameObject RulingPrefab;

    public bool IsBold = false;

    void Start () {
        GeneratRulings();
    }
    
    SpriteRenderer rulingSprite = null;
    private IList<float> angleList = new List<float>();
    private void GeneratRulings()
    {
        if (null == gauge)
        {
            return;
        }

        int count = 0;
        float angle = 0f;

        float splitAngle = gauge.splitAngleThin;
        if (IsBold)
        {
            splitAngle = gauge.splitAngleBold;
        }

        count = (int)((gauge.maxAngle - gauge.minAngle) / splitAngle) + 1;
        for (int i = 0; i < count; i++)
        {
            //Right
            //angle = 360 - (gauge.minAngle + i * splitAngle) + 90;
            angle = 360 - (gauge.minAngle + i * splitAngle) + 90;
            if (angle >= 360)
            {
                angle = angle - 360;
            }            

            angleList.Add(angle);
            //Debug.LogFormat("[{0}] -> {1}, {2}", i, gauge.minAngle + i * splitAngle, angle);
        }
        //===
        GameObject newInstance;
        
        for (int i = 0; i < count; i++)
        {
            angle = angleList[i];

            newInstance = Instantiate<GameObject>(RulingPrefab);
            newInstance.transform.SetParent(transform);

            rulingSprite = newInstance.GetComponent<SpriteRenderer>();
            rulingSprite.transform.localScale = RulingScale;
            rulingSprite.transform.localPosition = Vector3.zero;

            rulingSprite.transform.localRotation = Quaternion.Euler(0, 0, angle);
        }
    }

}
